<?php
namespace Lib;

/**
 * Retrieve resources registered in application
 * 
 * @throws ResLoaderException
 */
class ResLoader
{
    /**
     * @var \Zend_Application_Bootstrap_Bootstrap
     */
    private static $_bootstrap = null;

    /**
     * Get registered resource from Application resource scope
     * @param string $resource Name of resource
     * @return \Zend_Application_Resource_ResourceAbstract
     * @throws ResLoaderException
     */
    public static function getResource($resource)
    {
        $bootstrap = self::getBootstrap();
        if(!$bootstrap->hasResource($resource)){
            try{
                return self::getBootstrap()->getApplication()->getBootstrap()->getResource($resource);
            } catch (Exception $e){
                throw new ResLoaderException('Resource '. (string) $resource. ' not founded');
            }
        } else {
            return $bootstrap->getResource($resource);
        }
    }

    /**
     * Returns application bootstrap
     *
     * @static
     * @return \Zend_Application_Bootstrap_Bootstrap
     */
    public static function getBootstrap()
    {
        if(null === self::$_bootstrap){
            // Retrieving Application bootstrap. !!! Not Front Controller bootstrap
            self::$_bootstrap = \Zend_Controller_Front::getInstance()->getParam('bootstrap');
        }
        return self::$_bootstrap;
    }
    
    /**
     * Get registered resource names
     *
     * @return array
     */
    public static function getResourceNames(){
        return self::getBootstrap()->getPluginResourceNames();
    }

    /**
     * Checks if resourse was registered
     *
     * @return bool
     */
    public static function hasResource($resource){
        return self::getBootstrap()->hasResource($resource);
    }

    
    /**
     * Get a bootstrap option
     * @deprecated
     * @param string $option
     * @return mixed
     */
    public static function getOption($option)
    {
        $bootstrap = self::getBootstrap();
        return $bootstrap->getOption($option);
    }

    /**
     * Get a bootstrap option
     *
     * @deprecated
     * @param string $option
     * @return mixed
     */
    public static function getDoctrine()
    {
        return self::getResource('Doctrine2');
    }
    
    /**
     * @deprecated
     * Enter description here ...
     */
    public static function getCache(){
        return self::getResource('Redis');
    }
    
/**
     * Get a bootstrap option
     *
     * @deprecated
     * @param string $option
     * @return mixed
     */
    public static function getLayout()
    {
        return self::getResource('layout');
    }
    
    /**
     * Get an item from the bootstrap container (Registry)
     *
     * @param string $item
     * @deprecated
     * @return mixed
     */
    public static function getItem($item)
    {
        $bootstrap = self::getBootstrap();
        return $bootstrap->getContainer()->$item;
    }
}

/**
 * 
 */
class ResLoaderException extends \Exception{}